import UIAbility from '@ohos.app.ability.UIAbility';
import hilog from '@ohos.hilog';
import window from '@ohos.window';
import relationalStore from '@ohos.data.relationalStore'
import resourceManager from '@ohos.resourceManager';
import buffer from '@ohos.buffer';
export default class EntryAbility extends UIAbility {
  static readonly BACKGROUND_COLOR = "#F5F6FA"
  onCreate(want, launchParam) {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
  }

  onDestroy() {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
  }

  onWindowStageCreate(windowStage: window.WindowStage) {
    // 初始化数据库
    this.initDatabase()
    // Main window is created, set main page for this ability
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
    // 1.获取应用主窗口。
    let windowClass = null;
    windowStage.getMainWindow((err, data) => {
      if (err.code) {
        console.error('Failed to obtain the main window. Cause: ' + JSON.stringify(err));
        return;
      }
      windowClass = data;
      console.info('Succeeded in obtaining the main window. Data: ' + JSON.stringify(data));
      //
      // //设置沉浸式效果
      // windowClass.setWindowLayoutFullScreen(true,(error,data) => {
      //   if(error.code == 0){
      //     console.log("设置成功")
      //   }else{
      //     console.log("设置失败")
      //   }
      // })
      const statusBarHeight = windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM).topRect.height
      hilog.info(0x0000, 'onWindowStageCreate', '%{public}s', 'statusBarHeight:' + statusBarHeight);
      globalThis.statusBarHeight = statusBarHeight
      windowStage.loadContent('pages/Index', (err, data) => {
        if (err.code) {
          hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
          return;
        }
        // 设置全局背景色
        windowClass.setWindowBackgroundColor(EntryAbility.BACKGROUND_COLOR)
        hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
      });
    })
  }

  onWindowStageDestroy() {
    // Main window is destroyed, release UI related resources
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
  }

  onForeground() {
    // Ability has brought to foreground
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
  }

  onBackground() {
    // Ability has back to background
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
  }


  private initDatabase() {
    const STORE_CONFIG = {
      name: 'LoveLedger.db', // 数据库文件名
      securityLevel: relationalStore.SecurityLevel.S1 // 数据库安全级别
    };
    const SQL_CREATE_TABLE_CHAT = 'CREATE TABLE IF NOT EXISTS CHAT (' +
    'ID INTEGER PRIMARY KEY AUTOINCREMENT,' +
    'DESCRIPTION TEXT NOT NULL,' +
    'VOICE_URL TEXT NOT NULL,' +
    'CREATE_TIME TEXT NOT NULL,' +
    'LOCATION TEXT,' +
    'DECIBEL_LEVEL INTEGER' +
    ')'
    const SQL_CREATE_TABLE = SQL_CREATE_TABLE_CHAT

    relationalStore.getRdbStore(this.context, STORE_CONFIG, (err, store) => {
      if (err) {
        console.error(`Failed to get RdbStore. Code:${err.code}, message:${err.message}`);
        return;
      }
      console.info(`Succeeded in getting RdbStore.`);
      store.executeSql(SQL_CREATE_TABLE); // 创建数据表

    });
    // 读取配置文件
    this.getConfigs();
  }

  private async getConfigs() {
    let data = await this.context.resourceManager.getRawFileContent("account.json")
    if(data) {
      const configs = JSON.parse(buffer.from(data).toString())
      globalThis.HUAWEI_PROJECT_ID = configs.huawei_project_id
      globalThis.HW_AUDIO_AK = configs.hw_audio_ak
      globalThis.HW_AUDIO_SK = configs.hw_audio_sk
      console.log("=========", configs)
    }
  }
}
